package com.cloudbroker.bcs.platform.ltb.common.payUtil;

import org.apache.log4j.Logger;
import sun.misc.BASE64Decoder;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;

/**
 * @ClassName CashierConfig
 * @Description 商户公钥私钥初始化
 * @author jiangchuan@hundsun.com
 * 
 *         <pre>
 *         说明： 以下代码只是为了方便商户测试而提供的样例代码，商户可以根据自己网站的需要，按照技术文档编写,并非一定要使用该代码。
 *         该代码仅供学习和研究三峡付接口使用，只是提供一个
 * @date 2014年10月17日
 */
public class ConfigUtil {
	private static final Logger loggger = Logger.getLogger(ConfigUtil.class);
	/**
	 * 签名算法
	 */
	public static final String SIGNATURE_ALGORITHM = "SHA1withRSA";
	/**
	 * 字符编码
	 */
	public static final String CHARSET = "UTF-8";
	public static final String ALGORITHM = "RSA";

	/**
	 * 实例化商户公钥
	 * 
	 * @return
	 */
	public static PublicKey getMerPubKey(String key) {
		PublicKey publicKey = null;
		try {
			if (null == key || key.length() == 0) {
				key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCZHwymO+mohDNQAC+pxcPjDCP8czo7wPGr2Ib8RUdlPlxlEchpY/WJuAidQDXhik96vLlQQJtRZ3d+RM2zPJeunj5aS5gP9KaGtg/XFl5tCoY9uGyGayK/ZfAh3cF/xvaPI9sILFXDKL6YcV37T4Dh+IxSk0rQMGTFMUSd39BuoQIDAQAB";
			}
			java.security.spec.X509EncodedKeySpec bobPubKeySpec = new java.security.spec.X509EncodedKeySpec(
					new BASE64Decoder().decodeBuffer(key));
			// RSA对称加密算法
			KeyFactory keyFactory;
			keyFactory = KeyFactory.getInstance("RSA");
			// 取公钥匙对象
			publicKey = keyFactory.generatePublic(bobPubKeySpec);
		} catch (NoSuchAlgorithmException e) {
			loggger.error(e.getMessage(), e);
		} catch (InvalidKeySpecException e) {
			loggger.error(e.getMessage(), e);
		} catch (IOException e) {
			loggger.error(e.getMessage(), e);
		}
		return publicKey;
	}

	/**
	 * 实例化商户私钥
	 * 
	 * @return
	 */
	public static PrivateKey getMerPrivateKey(String key) {
		PrivateKey privateKey = null;
		if (null == key || key.length() == 0) {
			key = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJkfDKY76aiEM1AAL6nFw+MMI/xz"
					+ "OjvA8avYhvxFR2U+XGURyGlj9Ym4CJ1ANeGKT3q8uVBAm1Fnd35EzbM8l66ePlpLmA/0poa2D9cW"
					+ "Xm0Khj24bIZrIr9l8CHdwX/G9o8j2wgsVcMovphxXftPgOH4jFKTStAwZMUxRJ3f0G6hAgMBAAEC"
					+ "gYBX57NjW9HwpLSwoOq8FIU4u6eNLrmnxET52vzjpwIt5Xc1rtfXaUN1JOoNHOfR641lcSMSJ4UD"
					+ "XtsC6o0M8SRCeeFo+axNEEvl//mlNVdhMzPXNjcqr8IJi2Nt1vAo8YsRYai4kJJ3iyQ6mO1Y+9ig"
					+ "A9JmkDMEY/r9iPnZ2oSSgQJBAPi5IgJOXqQUmD5jtCAXeYjphjENoIWPOF9ynoepCfi2yzJlQ0zu"
					+ "l7308i4Ka9gD/P9Rf3fYW9GjdY/Dx8uREm0CQQCdmeKWVF7DMzql1ZRagxwmgspLV30solMPHIEk"
					+ "kydbCnSMF0Hzoa9FXcco+yXNsweCKunzl1RC47PevrUr78yFAkAl2lcYrsCsjA3XgOkK8Uen1JJ9"
					+ "qKtDaFaWACWsU8eWuIsYMl0Zuj4RxNOOujtLdt/sI4UYwi7HKu9bX9pqWrvJAkATlZvisnlUKHmR"
					+ "Qgk9di15W6dn1WdotIM9zXz+3op1dccl4X4AmIb/R7gID62S8jxsoIiSDKwb48/mTiyVWSdBAkEA"
					+ "8WIPQHevODL4JazRFbCVfr83pVXxqYY7fytZGXCLcMhMjPthM4TU11CWQ7bj0GEq1nwP5nGJvGD3NSaIBmzh7w==";
		}
		PKCS8EncodedKeySpec priPKCS8;
		try {
			priPKCS8 = new PKCS8EncodedKeySpec(
					new BASE64Decoder().decodeBuffer(key));
			KeyFactory keyf = KeyFactory.getInstance(ALGORITHM);
			privateKey = keyf.generatePrivate(priPKCS8);
		} catch (IOException e) {
			loggger.error(e.getMessage(), e);
		} catch (NoSuchAlgorithmException e) {
			loggger.error(e.getMessage(), e);
		} catch (InvalidKeySpecException e) {
			loggger.error(e.getMessage(), e);
		}
		return privateKey;
	}

	/**
	 * 
	 * @Method: getWAPPublicKey
	 * @Description: WAP端URL加密公钥
	 * @param @return
	 * @return String
	 * @throws
	 */
	public static String getWAPPublicKey(String key) {
		if (null == key || key.length() == 0)
			return "ACED00057372002D6F72672E626F756E6379636173746C652E6A63652E70726F76696465722E4A43455253415075626C69634B657925226A0E5BFA6C840200024C00076D6F64756C75737400164C6A6176612F6D6174682F426967496E74656765723B4C000E7075626C69634578706F6E656E7471007E00017870737200146A6176612E6D6174682E426967496E74656765728CFC9F1FA93BFB1D030006490008626974436F756E744900096269744C656E67746849001366697273744E6F6E7A65726F427974654E756D49000C6C6F776573745365744269744900067369676E756D5B00096D61676E69747564657400025B42787200106A6176612E6C616E672E4E756D62657286AC951D0B94E08B0200007870FFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFE00000001757200025B42ACF317F8060854E0020000787000000080BABA07C98BE7AB7CCDAC276F00FED52C98757C9D4A26EC12C3E86F1A42DB6447E54F81E8F40DE63F41F5CE5A46F8783E6F0F10AAAEE8FC810EBB4087DF77842743C4CAF8BBAA837A14E6DC0E502C2756174FD7D142563FB38673A7C41A00BDB21840D78EAC773564C7C423D599CBCF12AF6F3FD117E983B7C5F4FF29EA976915787371007E0003FFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFE000000017571007E00070000000301000178";
		else {
			return key;
		}
	}

	public static PublicKey getSanxiapayPublicKey(String path) {
		if (null == path || path.isEmpty()) {
			path = "";
		}
		File file = new File(path);

		try {
			CertificateFactory certificate_factory = CertificateFactory
					.getInstance("X.509");
			InputStream inputStream = new FileInputStream(file);
			Certificate x509certificate = certificate_factory
					.generateCertificate(inputStream);
			return x509certificate.getPublicKey();

		} catch (Exception e) {
			loggger.error(e);
		}
		return null;
	}
}
